package com.java.service;

import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.java.entity.Code;

/**
 * 系统编码服务
 * @author wqf
 * @version  Nov 24, 2011 11:51:06 AM
 */
@Transactional(propagation = Propagation.REQUIRED,readOnly=false, rollbackFor = Exception.class)
public class CodeService extends BaseService{

	/**
	 * 添加编码
	 * @param code
	 */
	public void addCode(Code code){
		this.dao.saveObject(code);
	}
	
	/**
	 * 编辑（for dwr）
	 * @param code
	 */
	public void editCode(Code code){
		if(null == code.getId()){
			this.addCode(code);
		}else{
			this.updateCode(code);
		}
	}	
	
	/**
	 * 修改
	 * @param code
	 */
	public void updateCode(Code code){
		this.dao.updateObject(code);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void deleteCode(Long id){
		Code code = this.dao.loadObject(Code.class, id);
		this.dao.deleteObject(code);
	}

	public void deleteCodeByIds(String ids){
		if(StringUtils.isNotBlank(ids)){
			String []id  = ids.split(",");
			for(String s:id){
				deleteCode(Long.parseLong(s));
			}			
		}	
	}
	
	public Code getCodeById(Long id){
		return this.dao.loadObject(Code.class, id);
	}
	
	public boolean existsCode(String type,String value){
		Code code = this.dao.loadObject(Code.class, "codeType=? and codeName=?", type,value);
		if(null==code) return false;
		return true;
	}
}
